//
//  RechargeBtcViewController.m
//  BTCoin
//
//  Created by Robin on 26/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "RechargeBtcViewController.h"
#import <CoreImage/CoreImage.h>
#import "UIImage+Extension.h"
#import "UIButton+Extension.h"
#import "FinancialService.h"
#import "SliderMenuView.h"
#import "BCTRechargeView.h"
#import "LTCRechargeView.h"
#import "ETHRechargeView.h"
#import "ETCRechargeView.h"
#import "RechargeBtcModel.h"

#define kMarketViewH        40

@interface RechargeBtcViewController ()
@property (nonatomic, strong) SliderMenuView *menuView;
@property (nonatomic, strong) BCTRechargeView * BCTView;
@property (nonatomic, strong) LTCRechargeView * LTCView;
@property (nonatomic, strong) ETHRechargeView * ETHView;
@property (nonatomic, strong) ETCRechargeView * ETCView;
@property (nonatomic, strong) RechargeBtcModel * btcModel;
@property (nonatomic, strong) RechargeBtcModel * ltcModel;
@property (nonatomic, strong) RechargeBtcModel * ethModel;
@property (nonatomic, strong) RechargeBtcModel * etcModel;
@property (nonatomic, assign) CoinType coinType;
@end

@implementation RechargeBtcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleText:@"充值"];
    
    [self initUIHead];
    [self requestBCTRecharge];
    [self requestLTCRecharge];
    [self requestETHRecharge];
    [self requestETCRecharge];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initUIHead {
    @WeakObj(self)
    UIButton * btnRight = [UIButton initWithFrame:Rect(0,StatusBarHeight,100,44) title:@"充值记录"];
    [btnRight handleControlEvent:UIControlEventTouchUpInside withBlock:^{
//        [selfWeak showOpenIllustationDataSource];
    }];
    [self setRightBtn:btnRight];
    
    [self initMenuView];
    [self initSlideView];
}

- (void)initSlideView{
    
    @WeakObj(self)
    CGRect rect = CGRectMake(0,CGRectGetMaxY(self.headView.frame), kScreenWidth,ScreenHeight-CGRectGetMaxY(self.headView.frame));
    _menuView = [[SliderMenuView alloc] initWithFrame:rect withTitles:@[@"BCT充值",@"LTC充值",@"ETH充值",@"ETC充值"] withDefaultSelectIndex:1 withType:SliderMenuViewType_Normal];
    [_menuView setTitleLineViewHide:NO];
    _menuView.viewArrays = @[self.BCTView,self.LTCView,self.ETHView,self.ETCView];
    [self.view addSubview:_menuView];
    
    self.menuView.DidSelectSliderIndex = ^(NSInteger index){
        @StrongObj(self);
        if (1 == index) {
            self.coinType = kCoinTypeBTC;
        } else if (2 == index) {
            self.coinType = kCoinTypeLite;
        } else if (3 == index) {
            self.coinType = kCoinTypeEther;
        } else if (4 == index) {
            self.coinType = kCoinTypeEtherClassic;
        }
    };
}

- (void)initMenuView {
    CGRect frame = Rect(0, CGRectGetMaxY(self.headView.frame)+kMarketViewH, kScreenWidth, ScreenHeight-(CGRectGetMaxY(self.headView.frame)+kMarketViewH+TabBarHeight));
    self.BCTView = [[BCTRechargeView alloc] initWithFrame:frame];
    self.BCTView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.LTCView = [[LTCRechargeView alloc] initWithFrame:frame];
    self.LTCView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.ETHView = [[ETHRechargeView alloc] initWithFrame:frame];
    self.ETHView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.ETCView = [[ETCRechargeView alloc] initWithFrame:frame];
    self.ETCView.backgroundColor = UIColorFromRGB(0xF9F9F9);
}

#pragma mark - Service
- (void)requestBCTRecharge {
    Loading_Gold_Show(self.view);
    NSDictionary * parDict = @{@"currentPage":@(1),
                               @"symbol":@(kCoinTypeBTC)
                               };
    [[FinancialService sharedFinancialService] rechargeBtc:parDict success:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.btcModel = [[RechargeBtcModel alloc] initWithDictionary:responseObject error:nil];
            
            [self updateBCTUI];
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

- (void)requestLTCRecharge {
    NSDictionary * parDict = @{@"currentPage":@(1),
                               @"symbol":@(kCoinTypeLite)
                               };
    [[FinancialService sharedFinancialService] rechargeBtc:parDict success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.ltcModel = [[RechargeBtcModel alloc] initWithDictionary:responseObject error:nil];
            
            [self updateLTCUI];
        }
    } fail:^(NSError *error) {
    }];
}

- (void)requestETHRecharge {
    NSDictionary * parDict = @{@"currentPage":@(1),
                               @"symbol":@(kCoinTypeEther)
                               };
    [[FinancialService sharedFinancialService] rechargeBtc:parDict success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.ethModel = [[RechargeBtcModel alloc] initWithDictionary:responseObject error:nil];
            
            [self updateETHUI];
        }
    } fail:^(NSError *error) {
    }];
}

- (void)requestETCRecharge {
    NSDictionary * parDict = @{@"currentPage":@(1),
                               @"symbol":@(kCoinTypeEtherClassic)
                               };
    [[FinancialService sharedFinancialService] rechargeBtc:parDict success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.etcModel = [[RechargeBtcModel alloc] initWithDictionary:responseObject error:nil];
            
            [self updateETCUI];
        }
    } fail:^(NSError *error) {
    }];
}

#pragma mark - private methods
- (void)updateBCTUI {
    if (!self.btcModel.fvirtualaddress.fadderess) {
        return;
    }
    UIImage * image = [self creatCIQRCodeImage:self.btcModel.fvirtualaddress.fadderess];
    self.BCTView.codeImageView.image = image;
    self.BCTView.addressLabel.text = self.btcModel.fvirtualaddress.fadderess;
}

- (void)updateLTCUI {
    if (!self.ltcModel.fvirtualaddress.fadderess) {
        return;
    }
    
    UIImage * image = [self creatCIQRCodeImage:self.ltcModel.fvirtualaddress.fadderess];
    self.LTCView.codeImageView.image = image;
    self.LTCView.addressLabel.text = self.ltcModel.fvirtualaddress.fadderess;
}

- (void)updateETHUI {
    if (!self.ethModel.fvirtualaddress.fadderess) {
        return;
    }
    
    UIImage * image = [self creatCIQRCodeImage:self.ethModel.fvirtualaddress.fadderess];
    self.ETHView.codeImageView.image = image;
    self.ETHView.addressLabel.text = self.ethModel.fvirtualaddress.fadderess;
}

- (void)updateETCUI {
    if (!self.etcModel.fvirtualaddress.fadderess) {
        return;
    }
    
    UIImage * image = [self creatCIQRCodeImage:self.etcModel.fvirtualaddress.fadderess];
    self.ETCView.codeImageView.image = image;
    self.ETCView.addressLabel.text = self.etcModel.fvirtualaddress.fadderess;
}


/**
 *  生成二维码
 */
- (UIImage *)creatCIQRCodeImage:(NSString *)message
{
    // 1.创建过滤器，这里的@"CIQRCodeGenerator"是固定的
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复默认设置
    [filter setDefaults];
    
    // 3. 给过滤器添加数据
    NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
    // 注意，这里的value必须是NSData类型
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 4. 生成二维码
    CIImage *outputImage = [filter outputImage];
    
    // 5. 显示二维码
    UIImage * retImage = [UIImage creatNonInterpolatedUIImageFormCIImage:outputImage withSize:200.0];
    return retImage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
